# -*- coding: utf-8 -*-
"""
Created on Thu May 30 19:02:14 2013

@author: Maxim
"""
import mass
import matplotlib.pyplot as plt
from stability_report import StabilityReport
import aircraft
import numpy as np

def main():
    passenger  = mass.MassComponent('passenger',86.0,[1.86,0.25,-0.075])
    pilotHeavy = mass.MassComponent('pilot',    86.0,[1.86,-0.25,-0.075])
    pilotLight = mass.MassComponent('pilot',    55.0,[1.86,-0.25,-0.075])
    baggage    = mass.MassComponent('baggage',  70.0,[2.85,0,-0.125])
    fuelFull   = mass.MassComponent('fuel',     95.0,[1.81,0.0,-0.3366])
    fuelOper   = mass.MassComponent('fuel',     18.0,[1.81,0.0,-0.3366])
    fuelMin    = mass.MassComponent('fuel',     5.0,[1.81,0.0,-0.3366])
    airframe   = mass.MassComponent('airframe', 360.1,[1.806,-0.0013,-0.1301])
    
    load1 = mass.MassList('case 1')
    load1.add_component(airframe)
    load1.add_component(fuelFull)
    load1.add_component(passenger)
    load1.add_component(pilotHeavy)
    load1.add_component(baggage)
    
    load2 = mass.MassList('case 2')
    load2.add_component(airframe)
    load2.add_component(fuelOper)
    load2.add_component(passenger)
    load2.add_component(pilotHeavy)
    load2.add_component(baggage)
    
    load3 = mass.MassList('case 3')
    load3.add_component(airframe)
    load3.add_component(fuelFull)
    load3.add_component(pilotHeavy)
    load3.add_component(baggage)
    
    load4 = mass.MassList('case 4')
    load4.add_component(airframe)
    load4.add_component(fuelFull)
    load4.add_component(pilotHeavy)
    
    load5 = mass.MassList('case 5')
    load5.add_component(airframe)
    load5.add_component(fuelOper)
    load5.add_component(pilotHeavy)
    load5.add_component(baggage)
    
    load6 = mass.MassList('case 6')
    load6.add_component(airframe)
    load6.add_component(fuelOper)
    load6.add_component(pilotHeavy)
    
    load7 = mass.MassList('case 7')
    load7.add_component(airframe)
    load7.add_component(fuelMin)
    load7.add_component(pilotLight)

    cases = [load1,load2,load3,load4,load5,load6,load7]

    ac = aircraft.load('V0510')
    sr = StabilityReport(ac)
    sr.init_report()
    sr.add_flight_condition(0.0,[20.,70.0,10])
    sr.reportStaticMargin = True
    sr.set_required_param('derivatives',['Clb','Cnb','Cma'],['Cl_beta','Cn_beta','Cm_alpha'])
    sr.set_required_param('basic',['elevator','xNP'],['elev deflection,deg','neutral point'])
    
    cg = np.zeros(len(cases))
    m  = np.zeros(len(cases))
    for i,case in enumerate(cases):
        sr.add_cg(case.CG,case.get_total_mass(),1.0,case.name)
        cg[i] = case.get_CG()[0]
        m[i]  = case.get_total_mass()
    sr.plot = True
    sr.report = True
    sr.generate(14)
    
    #---
    xnp = sr.get_data('basic','xNP')
    xnp = xnp.sum()/len(xnp)
    smTarget = [0.08,0.08, 0.20,0.20]
    cgTarget = xnp - smTarget*ac.wing.MAC
    massTarget = [0.0,620.0,620.0,0.0]

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.hold(True)
    ax.grid(True)
    ax.plot(cg,m,'bD')
    ax.plot(cgTarget,massTarget,'ro-')
    ax.plot(xnp*np.ones(10),np.linspace(0.0,620,10),'k-*')
    ax.legend(['current V100','target','neutral point'],'lower right')
    ax.set_xlabel('x CG, mm')
    ax.set_ylabel('Mass, kg')
    plt.show()


    
def new_report():
    ac = aircraft.load('V0510')
    sr = StabilityReport(ac)
    sr.init_report()
    sr.add_cg([1.656,0.0,0.0],726.0,1.0,'current FWD')
    sr.add_cg([1.800,0.0,0.0],726.0,1.0,'current AFT')
    
    sr.add_flight_condition(0.0,[20.0,75.0,15])
    sr.reportStaticMargin = True
    sr.set_required_param('derivatives',['Clb','Cnb','Cma'],['Cl_beta','Cn_beta','Cm_alpha'])
    sr.set_required_param('basic',['elevator','xNP'],['elev deflection,deg','neutral point'])
    sr.generate(14)


if __name__=="__main__":
    new_report()
    
    